package entity;

import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.LinkedList;
import java.util.Objects;

/**
 * 一场百家乐牌局
 */
@Data
public class Poker {
    /**
     * 牌局胜利者
     */
    Integer winner;
    /**
     * 庄家点数
     */
    Integer bankerPoint;
    /**
     * 闲家点数
     */
    Integer playerPoint;
    /**
     * 庄家当前牌局卡牌
     */
    LinkedList<Card> banker;
    /**
     * 闲家当前牌局卡牌
     */
    LinkedList<Card> player;

    /**
     * 无参构造函数初始化字段
     */
    public Poker() {
        winner = 0;
        bankerPoint = 0;
        playerPoint = 0;
        banker = new LinkedList<>();
        player = new LinkedList<>();
    }

    /**
     * 比对谁是胜利者，并记录结果
     *
     * @param count 记录结果的对象
     */
    public void win() {
        if (Objects.equals(bankerPoint, playerPoint)) {
            winner = 2;
        } else if (bankerPoint < playerPoint) {
            winner = 1;
        } else {
            winner = 0;
        }
    }

    /**
     * 根据卡牌技术闲家点数
     */
    public void countPlayer() {
        playerPoint = 0;
        for (Card baccarat : player) {
            switch ((int) baccarat.getRank()) {
                case 65:
                    playerPoint++;
                    break;
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    playerPoint += (int) baccarat.getRank();
                    break;
            }
        }
        while (playerPoint > 9) {
            playerPoint -= 10;
        }
    }

    /**
     * 展示当前牌局结果
     */
    public void display() {
        System.out.printf("%-3s%-2s\n", "庄", bankerPoint);
        for (Card baccarat : banker) {
            if ((int) baccarat.getRank() > 10) {
                System.out.printf("%-3s", baccarat.getRank());
            } else {
                System.out.printf("%-3s", (int) baccarat.getRank());
            }
        }
        System.out.println();
        System.out.printf("%-3s%-2s\n", "闲", playerPoint);
        for (Card baccarat : player) {
            if ((int) baccarat.getRank() > 10) {
                System.out.printf("%-3s", baccarat.getRank());
            } else {
                System.out.printf("%-3s", (int) baccarat.getRank());
            }
        }
        System.out.println();
        System.out.printf("%-3s", winner);
        System.out.println("\n");
    }

    /**
     * 根据卡牌计算庄家点数
     */
    public void countBanker() {
        bankerPoint = 0;
        for (Card baccarat : banker) {
            switch (baccarat.getRank()) {
                case 65:
                    bankerPoint++;
                    break;
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    bankerPoint += (int) baccarat.getRank();
                    break;
            }
        }
        while (bankerPoint > 9) {
            bankerPoint -= 10;
        }
    }


}
